package it.koutifaris.travianmap.importer;

import it.koutifaris.travianmap.db.DomainUpdaterFactory;
import it.koutifaris.travianmap.db.DomainUpdaterFactory.RevisionDAO;
import it.koutifaris.travianmap.db.operation.DomainOperationFactory;
import it.koutifaris.travianmap.db.operation.OperationType;
import it.koutifaris.travianmap.mapsqlparser.MapSqlEntry;
import it.koutifaris.travianmap.mapsqlparser.MapSqlEntryHandler;

import java.util.Date;

public class DBImporter implements MapSqlEntryHandler {

	private MapSqlEntryExploder entryExploder = new MapSqlEntryExploder();

	private DomainUpdaterFactory domainDAL;// new
	private DomainOperationFactory operationFactory;// new
	private OperationType operationType;

	public DBImporter() {
		Date today = new Date();

		RevisionDAO revisionDAO = domainDAL.createRevisionDAO();
		Integer revision = revisionDAO.retreiveRevision(today);
		operationType = OperationType.INSERT;
		if (revision == null) {
			operationType = OperationType.UPDATE;
			revision = revisionDAO.createRevision(today);
		}

		// operationFactory = factory.createOperationFactory(domainDAL.createDomainUpdaterDAO(revision.intValue()));
	}

	@Override
	public boolean onEntry(MapSqlEntry entry) {
		entryExploder.explode(entry);

		operationFactory.createOperation(operationType, entryExploder.getAlliance()).doOperation();
		operationFactory.createOperation(operationType, entryExploder.getPlayer()).doOperation();
		operationFactory.createOperation(operationType, entryExploder.getVillage()).doOperation();

		return true;
	}
}
